﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Security.Cryptography;

namespace Utility
{
    public class TextHelper
    {
        #region 过滤特殊字符

        /// <summary>
        /// 过滤特殊字符
        /// </summary>
        /// <param name="source">需要过滤的内容</param>
        /// <returns></returns>
        public static string Filter(string source)
        {
            //需要过滤的词语 

            string express = "胡锦涛|fuck|FUCK|bitch|BITCH|他妈的|法轮|falundafa|FALUNDAFA|falun|FALUN|江泽民|操你妈|三级片|Fa轮功|fa轮功|日你|我日|suck|SUCK|shit|SHIT|法轮|我操|李宏治|阴茎|傻B|妈的|操你|干你|日您|屁眼|反共|国民党|共匪|台独|操|炮友|温家宝|郭台铭|台铭|肏|屄|草泥马|屌|跳楼|阳具|装逼|傻逼|我靠|打炮|妓女|李洪志|洪志|疆独|藏独|骚包|骚货|鸡鸡|我靠|奝|日本人|棒子|韩国人|比亚迪|BYD|sb|SB|尻|吊";
            string[] list = express.Split('|');
            foreach (string s in list)
            {
                source = source.Replace(s, "**");
            }
            return source;
        }
        #endregion
        #region 过滤SQL注入
        public static string String_FilterSQLAttack(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }
            else
            {
                //删除脚本
                str = Regex.Replace(str, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
                //删除HTML
                str = Regex.Replace(str, @"-->", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, @"<!--.*", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, "xp_cmdshell", "", RegexOptions.IgnoreCase);

                //删除与数据库相关的词
                str = Regex.Replace(str, "select", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, "insert", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, "delete from", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, "update", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, "count''", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, "drop table", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, "truncate", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, "asc", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, "mid", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, "char", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, "xp_cmdshell", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, "exec master", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, "net localgroup administrators", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, "and", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, " or", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, "net user", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, "net", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, "--", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, "delete", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, "drop", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, "script", "", RegexOptions.IgnoreCase);

                //特殊的字符
                str = str.Replace("--", "");
                str = str.Replace("*/", "");
                str = str.Replace("rn", "");
                return str;
            }

        }
        #endregion
        #region 过滤HTML代码
        public static string String_FilterHTMLAttack(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }
            else
            {
                //删除脚本
                str = Regex.Replace(str, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
                //删除HTML
                str = Regex.Replace(str, @"-->", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, @"<!--.*", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, "xp_cmdshell", "", RegexOptions.IgnoreCase);
                str = Regex.Replace(str, @"</*(\w+)/*>", "$1", RegexOptions.IgnoreCase);
                //特殊的字符
                str = str.Replace("--", "");
                str = str.Replace("*/", "");
                str = str.Replace("rn", "");
                return str;
            }
        }
        #endregion
        #region ZEN_MD5加密
        public static string Zen_encrypt_password(string plain)
        {
            string password1 = " ";
            string salt = " ";
            Random rand = new Random();

            for (int i = 0; i < 10; i++)
            {
                password1 += rand.Next().ToString();
            }

            salt = HashMD5(password1).Substring(0, 2);
            password1 = HashMD5(salt + plain) + ':' + salt;
            return password1;
        }

        public static string HashMD5(string input)
        {
            byte[] result = ((HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(System.Text.Encoding.UTF8.GetBytes(input));
            StringBuilder output = new StringBuilder(16);

            for (int i = 0; i < result.Length; i++)
            {
                output.Append((result[i]).ToString("x2", System.Globalization.CultureInfo.InvariantCulture));
            }
            return output.ToString();
        }
        #endregion
        #region 生成唯一字符串
        public static string Zen_Create_Guid()
        {
            long i = 1;
            foreach (byte b in Guid.NewGuid().ToByteArray())
            {
                i *= ((int)b + 1);
            }
            return string.Format("{0:x}", i - DateTime.Now.Ticks).ToUpper();
        }
        #endregion
    }
}
